package com.xhorse.lockplatform.controller;

import com.xhorse.lockplatform.annotation.RequestLimit;
import com.xhorse.lockplatform.comm.constant.ConstantDefineUtil;
import com.xhorse.lockplatform.comm.model.BaseMessage;
import com.xhorse.lockplatform.comm.mvc.AbstractCallbackController;
import com.xhorse.lockplatform.model.pojo.DataBean;
import com.xhorse.lockplatform.service.CreditsExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("Credits")
public class CreditsExchangeController extends AbstractCallbackController {

    @Autowired
    private CreditsExchangeService creditsExchangeService;

    /**
     * 获取绑定记录列表
     *
     * @param customerId
     * @return
     */
    @RequestLimit(count = 10, time = 60000)
    @RequestMapping(value = "getProductLogByUser", method = RequestMethod.POST)
    @ResponseBody
    public void getProductLogByUser(HttpServletRequest request, HttpServletResponse response,
                                    String customerId) throws Exception {
        List<DataBean> list = creditsExchangeService.getProductLogByUser(request, customerId);
        if (list == null) {
            simpleFailed(response);
        }
        simpleSuccess(response, list);
    }

    /**
     * 设备绑定
     *
     * @param customerId
     * @param sn
     * @return
     */
    @RequestLimit(count = 10, time = 60000)
    @RequestMapping(value = "bindingDevice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> bindingDevice(HttpServletRequest request,
                                             String customerId, String sn) {
        BaseMessage baseMessage = new BaseMessage();
        baseMessage.baseMsg = creditsExchangeService.getCredits(request,
                customerId, sn);
        if (baseMessage.baseMsg == null) {
            baseMessage.puterroMsg(ConstantDefineUtil.NETWORK_ERRORRMSG
                    .get("请求失败，请重试"));
        }
        return baseMessage.baseMsg;
    }

    /**
     * 设备解绑
     *
     * @param customerId
     * @param sn
     * @return
     */
    @RequestLimit(count = 10, time = 60000)
    @RequestMapping(value = "unbindingDevice", method = RequestMethod.POST)
    @ResponseBody
    public String unbindingDevice(HttpServletRequest request, String customerId, String sn) {
        return creditsExchangeService.unbindingDevice(request, customerId, sn);
    }

    // ==========================================================新增积分功能接口============================================================

    /**
     * 积分查询
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "queryForCredits", method = RequestMethod.POST)
    @ResponseBody
    public String queryForCredits(HttpServletRequest request) {
        return creditsExchangeService.queryForCredits(request);
    }

    /**
     * 获取对换列表
     *
     * @param typeId 1:点数兑换，2：功能兑换，3：物品兑换
     * @return
     */
    @RequestMapping(value = "queryProducts", method = RequestMethod.POST)
    @ResponseBody
    public String queryProducts(HttpServletRequest httpServletRequest) {
        return creditsExchangeService.selectProductByType(httpServletRequest);
    }

    /**
     * 获取经销商列表
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getDealerList", method = RequestMethod.POST)
    @ResponseBody
    public String getDealerList(HttpServletRequest httpServletRequest) {
        return creditsExchangeService.getDealerList(httpServletRequest);
    }

    /**
     * 提交订单
     *
     * @param httpServletRequest
     * @return 返回生成的订单号
     */
    @RequestLimit(count = 10, time = 10000)
    @RequestMapping(value = "uploadOrder", method = RequestMethod.POST)
    @ResponseBody
    public String uploadOrder(HttpServletRequest httpServletRequest, @RequestBody String requebody) {
        return creditsExchangeService.uploadOrder(httpServletRequest, requebody);
    }


    /**
     * 获取订单列表
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getOrderList", method = RequestMethod.POST)
    @ResponseBody
    public String getOrderList(HttpServletRequest httpServletRequest) {
        return creditsExchangeService.getOrderList(httpServletRequest);
    }

    /**
     * 获取客户产品列表（兑换功能）
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getFunctionProductList", method = RequestMethod.POST)
    @ResponseBody
    public String getFunctionProductList(HttpServletRequest httpServletRequest) {
        return creditsExchangeService.getFunctionProductList(httpServletRequest);
    }

    /**
     * 兑换客户产品列表（兑换点数）
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getPointsProductList", method = RequestMethod.POST)
    @ResponseBody
    public String getPointsProductList(HttpServletRequest httpServletRequest) {
        return creditsExchangeService.getPointsProductList(httpServletRequest);
    }


}
